/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hojadetrabajo9;

/**
 *
 * @author RAUL   tomado de internet de varias fuentes cmo : http://www.dgp.toronto.edu/~jstewart/270/9798s/Laffra/DijkstraApplet.html
 * http://www.vogella.de/articles/JavaAlgorithmsDijkstra/article.html, 
 */

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.InputMismatchException;

public class Driver9 {
    public static List<Edge> edges = new ArrayList<Edge>();
   
    public static List<Vertex> nodes = new ArrayList<Vertex>(); 
    
    public static void main(String[] args) {
      
    int initial = 0;
    int ingreso;
    Scanner scan = new Scanner(System.in);
    ingreso = (int)obtenerNumero("¿Que opcioon desea realizar?",true);
    
        for (int i = 0; i < 11; i++) {
            Vertex location = new Vertex("Node_" + i, "Node_" + i);
            nodes.add(location);
		}

                addLane("Edge_0", 0, 1, 85);
		addLane("Edge_1", 0, 2, 217);
		addLane("Edge_2", 0, 4, 173);
		addLane("Edge_3", 2, 6, 186);
		addLane("Edge_4", 2, 7, 103);
		addLane("Edge_5", 3, 7, 183);
		addLane("Edge_6", 5, 8, 250);
		addLane("Edge_7", 8, 9, 84);
		addLane("Edge_8", 7, 9, 167);
		addLane("Edge_9", 4, 9, 502);
		addLane("Edge_10", 9, 10, 40);
		addLane("Edge_11", 1, 10, 600);

		// Lets check from location Loc_1 to Loc_10
		Graphs graph = new Graphs(nodes, edges);
		Dijkstra dijkstra = new Dijkstra(graph);
           
		System.out.println("Estas son las conexiones de nodos y sus pesos ");
                for (int j = 0; j < 11; j++) {
                System.out.print(edges.get(j));
                System.out.print(" ");
                System.out.println(edges.get(j).getWeight());
                }
                System.out.println("Ingrese un valor entre 0 y 10 porque esos son los nodos ingresados previamente");
                initial = scan.nextInt();
		dijkstra.execute(nodes.get(initial));
                 
  //
             for (int k = 0; k < 11; k++) {   
                 if (!(initial == k)){
                     System.out.println();
                System.out.println("Ruta desde nodo " + initial + " hasta nodo " + k);
		LinkedList<Vertex> path = dijkstra.getPath(nodes.get(k));
                
               if (!(path == null)){ 
                for (Vertex vertex : path) {
			System.out.print(vertex);
                        System.out.print(" ");
                for (int i = 0; i < 11; i++) {
                    if (vertex.getId().equals(edges.get(i).getDestination().getId())){
                        System.out.println(edges.get(i).getWeight());
                        break;
                    }
                         
                }
             
		}}
               if ((path == null)){
               System.out.println("No existe conexion entre los nodos");
               }
                 }
            }
     
    }
       

        public static void addLane(String laneId, int sourceLocNo, int destLocNo, int duration) {
            Edge lane = new Edge(laneId,nodes.get(sourceLocNo), nodes.get(destLocNo), duration );
            edges.add(lane);
            }

            public static double obtenerNumero(String mensaje, boolean mostrar){
            Scanner ingreso = new Scanner(System.in);
            double num = -1;
        
            System.out.print(mensaje);
             try{
                num = ingreso.nextDouble();
            }
            catch(InputMismatchException exception){
                if (mostrar)
                    System.out.println("\nERROR: ~~> Dato invalido. :O(\n");
            }
            finally{
                ingreso.nextLine();//Atrapar el ENTER
            }
            return num;
            }

       }
